﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Collections.ObjectModel;
using System.Collections;

namespace CitrixPVSWrapper
{

    [System.Management.Automation.Cmdlet("RunWithReturn", "PvsServerStoreActiveDeviceCount", SupportsShouldProcess = true)]
    public class Run_PvsServerStoreActiveDeviceCount : System.Management.Automation.PSCmdlet
    {

        [Parameter(Mandatory = false)]
        public PvsStore Store;

        [Parameter(Mandatory = false)]
        public string StoreId;

        [Parameter(Mandatory = false)]
        public string StoreName;

        [Parameter(Mandatory = false)]
        public PvsServer Server;

        [Parameter(Mandatory = false)]
        public string ServerId;

        [Parameter(Mandatory = false)]
        public string ServerName;

        protected override void BeginProcessing()
        {
            // Test for mutually exclusive parameters
            Validate.MutuallyExclusiveParameters(this.MyInvocation.BoundParameters, new string[] { "Server", "ServerId", "ServerName" }, true);
            Validate.MutuallyExclusiveParameters(this.MyInvocation.BoundParameters, new string[] { "Store", "StoreId", "StoreName" }, true);

                        // Test that the supplied Server exists
            if (MyInvocation.BoundParameters.ContainsKey("Server")) 
            {
                if (Get_PvsServer_Internal.Get_ServerById(this.Server.ServerId).Count == 1)
                {
                    this.ServerId = this.Server.ServerId;
                    this.ServerName = this.Server.ServerName;
                }
                else
                {
                    throw new ParameterBindingException("The specified server could not be found");
                }
            }
            else if (MyInvocation.BoundParameters.ContainsKey("ServerId")) 
            {
                if (Get_PvsServer_Internal.Get_ServerById(this.ServerId).Count == 1)
                {
                    this.Server = Get_PvsServer_Internal.Get_ServerById(this.ServerId).First();
                    this.ServerName = this.Server.ServerName;
                }
                else
                {
                    throw new ParameterBindingException("The specified server Id could not be found");
                }
            }
            else if (MyInvocation.BoundParameters.ContainsKey("ServerName")) 
            {
                if (Get_PvsServer_Internal.Get_ServerByName(this.ServerName).Count == 1)
                {
                    this.Server = Get_PvsServer_Internal.Get_ServerByName(this.ServerName).First();
                    this.ServerId = this.Server.ServerId;
                }
                else
                {
                    throw new ParameterBindingException("The specified server name could not be found");
                }
            }

            // Test that the supplied Store exists
            if (MyInvocation.BoundParameters.ContainsKey("Store")) 
            {
                if (Get_PvsStore_Internal.Get_StoreById(this.Store.StoreId).Count == 1)
                {
                    this.StoreId = this.Store.StoreId;
                    this.StoreName = this.Store.StoreName;
                }
                else
                {
                    throw new ParameterBindingException("The specified store could not be found");
                }
            }
            else if (MyInvocation.BoundParameters.ContainsKey("StoreId")) 
            {
                if (Get_PvsStore_Internal.Get_StoreById(this.StoreId).Count == 1)
                {
                    this.Store = Get_PvsStore_Internal.Get_StoreById(this.StoreId).First();
                    this.StoreName = this.Store.StoreName;
                }
                else
                {
                    throw new ParameterBindingException("The specified store Id could not be found");
                }
            }
            else if (MyInvocation.BoundParameters.ContainsKey("StoreName")) 
            {
                if (Get_PvsStore_Internal.Get_StoreByName(this.StoreName).Count == 1)
                {
                    this.Store = Get_PvsStore_Internal.Get_StoreByName(this.StoreName).First();
                    this.StoreId = this.Store.StoreId;
                }
                else
                {
                    throw new ParameterBindingException("The specified store name could not be found");
                }
            }
        }

        protected override void ProcessRecord()
        {
            // Verbose and Debug Settings
            Boolean Verbose = this.MyInvocation.BoundParameters.ContainsKey("Verbose");
            WriteVerbose("Verbose Flag: " + Verbose);
            Boolean Debug = this.MyInvocation.BoundParameters.ContainsKey("Debug");
            WriteDebug("Debug Flag: " + Debug);

            // Collections to hold the queries and the results
            Collection<QueryDefinition> RunQueries = new Collection<QueryDefinition>();
            string TargetObject = "ServerStoreActiveDeviceCount";

            QueryDefinition RunQuery = new QueryDefinition(QueryActionType.RunWithReturn, TargetObject);
            RunQuery.Parameters.Add("serverId=\"" + this.ServerId + "\"");
            RunQuery.Parameters.Add("storeId=\"" + this.StoreId + "\"");

            String ShouldProcessMessage = String.Format("Get active device count for server {0} and store {1}", this.ServerName, this.StoreName);

            // Perform the actual run and return any errors
            if (ShouldProcess(ShouldProcessMessage))
            {
                foreach (string[] Record in PvsHelpers.ExecuteMcliQuery(RunQuery, Verbose, Debug))
                {
                    WriteDebug("Returning Object");
                    WriteObject(PvsHelpers.Get_UInt32ValueFromRecord("ServerStoreActiveDeviceCount", Record, Verbose));
                }
            }

        }
    }
}
